﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using Frame.Lib.Common;

namespace Frame.Lib.Utilities
{
    /// <summary>
    /// 所有需要各种转换的方法都在些类中
    /// </summary>
    public sealed class ConvertUtil
    {
        #region 将Int型ID集合转成逗号分隔的字符串,一般借Sql语句里In后面的调用
        /// <summary>
        /// 将Int型ID转成逗号分隔的字符串,一般借Sql语句里In后面的调用
        /// </summary>
        /// <param name="lstID"></param>
        /// <returns></returns>         
        public static string ConvertToDbStr<T>(IEnumerable<T> ids)
        {
            string str = "";
            if (typeof(T) == typeof(int) || typeof(T) == typeof(long))
            {
                StringBuilder sbIDs = new StringBuilder("null");
                foreach (T id in ids)
                {
                    sbIDs.Append("," + id);
                }
                str = sbIDs.ToString();
            }
            else if (typeof(T) == typeof(string))
            {
                StringBuilder sbStrs = new StringBuilder();
                foreach (T id in ids)
                {
                    sbStrs.Append(",'" + id + "'");
                }
                if (sbStrs.Length == 0)
                {
                    sbStrs.Append("newid()");
                }
                else
                {
                    sbStrs.Remove(0, 1);
                }
                str = sbStrs.ToString();
            }
            else
            {
                throw new Exception("数据类型不对");
            }
            return str;
        }
        #endregion

        /// <summary>
        /// 把ID为字符型的列表转换成数据库所需要字符串,一般在用在数据库in语句后面
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string ConvertToDbStr(IEnumerable<string> ids)
        {
            return ConvertToDbStr<string>(ids);
        }

        /// <summary>
        /// 把ID为整型的列表转换成数据库所需要字符串,一般在用在数据库in语句后面
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string ConvertToDbStr(IEnumerable<int> ids)
        {
            return ConvertToDbStr<int>(ids);
        }

        public static string ConvertToDbStr(IEnumerable<long> ids)
        {
            return ConvertToDbStr<long>(ids);
        }

        #region 把枚举转成两列的DT输出
        /// <summary>
        /// 把枚举转成两列的DT输出value为int, key 为string
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static List<EnumInfo> ConvertEnum(Type enumType)
        {
            List<EnumInfo> lstInfos = new List<EnumInfo>();

            int[] values = (int[])Enum.GetValues(enumType);
            foreach (int i in values)
            {
                EnumInfo info = new EnumInfo();
                info.ID = i;
                info.Name = Enum.GetName(enumType, i);
                lstInfos.Add(info);
            }
            return lstInfos;
        }
        #endregion


        /// <summary>
        /// 将集合转换成DataTable
        /// </summary>
        /// <typeparam name="Info"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable GetDataTable<Info>(IEnumerable<Info> list)
        {
            DataTable dt = new DataTable();
            Type type = typeof(Info);
            PropertyInfo[] pros = type.GetProperties();
            foreach (PropertyInfo tmp in pros)
            {
                if (tmp.PropertyType.Name != typeof(System.Data.Linq.EntitySet<>).Name)
                {
                    Type colType = Nullable.GetUnderlyingType(tmp.PropertyType) ?? tmp.PropertyType;//防止有可空的值类型
                    dt.Columns.Add(tmp.Name, colType);
                }
            }

            foreach (Info info in list)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dr[i] = type.GetProperty(dt.Columns[i].ColumnName).GetValue(info, null);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        #region 合并两个列表并去重
        /// <summary>
        /// 合并两个列表并去重
        /// </summary>
        /// <param name="lst1"></param>
        /// <param name="lst2"></param>
        /// <returns></returns>
        public static List<int> MergeList(List<int> lst1, List<int> lst2)
        {
            foreach (int tmp in lst2)
            {
                if (lst1.IndexOf(tmp) < 0)
                {
                    lst1.Add(tmp);
                }
            }
            return lst1;
        }
        #endregion

        public static List<int> ConvertToList(DataTable dt)
        {
            List<int> lstInt = new List<int>(dt.Rows.Count);
            foreach (DataRow dr in dt.Rows)
            {
                lstInt.Add((int)dr[0]);
            }
            return lstInt;
        }
    }
}
